import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author 后台赖骏
 * @version 1.0
 */
public class SearchFunction {

    public static boolean addTopic(){

        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        boolean execute = false;
        String sql = "insert into questions value (null,?,?,?)";
        try {
            Connection connection = JdbcUtils.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql);
            System.out.println("请输入你要增加的试题类型（tk：填空），（xz：选择）（dx，多选）");
            String questionType = scanner.next();
            ps.setNString(1, questionType);
            System.out.println("请输入你要增加的试题内容");
            String text = scanner.next();
            ps.setNString(2,text);
            System.out.println("请输入你要增加的试题答案");
            String answer = scanner.next();
            ps.setNString(3,answer);
            execute = ps.execute();
            //关闭流
            JdbcUtils.close(null,connection,ps);
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
        return execute;
    }

    public static void checkExam() throws SQLException, IOException {
        Connection connection = JdbcUtils.getConnection();
        //查询试题库
        String sql ="SELECT id,question,answer FROM `questions`";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
            System.out.println(rs.getInt(1)+"  "
                    +rs.getString(2)+"  答案  "+rs.getString(3));
        }
        //关闭流
        JdbcUtils.close(rs,connection,ps);
    }

    public static boolean deleteExam(String id) throws SQLException, IOException {
        Connection connection = JdbcUtils.getConnection();
        //删除试题库
        String sql ="Delete from questions where id = ?";

        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setNString(1,id);
        int execute = ps.executeUpdate();
        //关闭流
        JdbcUtils.close(null,connection,ps);
        return execute > 0 ;
    }

    public static void checkTime() throws SQLException, IOException {
        //获取连接
        Connection connection = JdbcUtils.getConnection();
        //查询试题库
        String sql ="SELECT * FROM test_time";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
            System.out.println(rs.getInt(1)+" 考试时间："
                    +rs.getString(2)+"  考试类型卷  "+rs.getString(3));
        }
        //关闭流
        JdbcUtils.close(rs,connection,ps);
    }
    public static boolean updateExam() throws SQLException, IOException {
        Connection connection = JdbcUtils.getConnection();
        //删除试题库
        String sql ="update questions set question = ?,answer = ? where id = ? ";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setNString(1,"10 + 10 = _______");
        ps.setNString(2,"20");
        ps.setNString(3,"2");
       int execute = ps.executeUpdate();
        //关闭流
        JdbcUtils.close(null,connection,ps);
        return execute > 0 ;
    }
}
